package com.hnpolice.xiaoke.carspeedcontrol.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

import com.hnpolice.xiaoke.carspeedcontrol.R;

/**
 * Created by Administrator on 2017/12/26 0026.
 * 自定义圆盘
 */

public class MyDicsView extends View {
    private float basex, basey;//中心圆点的x,y轴坐标
    private int mWidth, mHeight;
    private int raduisMax;//外圆的半径
    private int raduisMin;//内圆的半径
    private Paint mPaintRaMax, mPaintRaMin, mPaintText;//外圆的内画笔、内圆的画笔、文字的画笔
    private Context mContext;
    private Canvas mCanvas;
    //屏幕密度
    private float mDensityDpi;
    //速度
    private int speed = 0;
    //是否开启了任务
    private boolean isRunnaleCk = false;

    public MyDicsView(Context context) {
        super(context);
        initview(context);
    }

    public MyDicsView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initview(context);
    }

    public MyDicsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initview(context);
    }

    private void initview(Context context) {
        mContext = context;
        //获取屏幕宽高 和 屏幕密度dpi
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        mDensityDpi = displayMetrics.densityDpi / 320;
        //设置外圆的画笔
        mPaintRaMax = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintRaMax.setColor(mContext.getResources().getColor(R.color.colorAduisMax));
        mPaintRaMax.setAntiAlias(true);//去除锯齿
        mPaintRaMax.setStyle(Paint.Style.STROKE);
        mPaintRaMax.setStrokeWidth(10);
        //设置内圆的画笔
        mPaintRaMin = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintRaMin.setColor(mContext.getResources().getColor(R.color.colorAduisMin));
        mPaintRaMin.setAntiAlias(true);//去除锯齿
        mPaintRaMin.setStyle(Paint.Style.STROKE);
        mPaintRaMin.setStrokeWidth(10);
        //设置文字的画笔
        mPaintText = new Paint();
        mPaintText.setColor(mContext.getResources().getColor(R.color.colorText));
        mPaintText.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaintText.setTextSize(60 * mDensityDpi);
        // 获取字体并设置画笔字体
        Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), "kt.ttf");
        mPaintText.setTypeface(typeface);

    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        mCanvas = canvas;
        drawCircleView();
        drawArcView();
        drawTextView();
        drawCircleLineView();
    }

    /**
     * 绘制黑色的背景圆、外边框圆、内圆
     */
    private void drawCircleView() {
        mPaintRaMax.setStrokeWidth(10);
        Paint paintBlack = new Paint();
        paintBlack.setColor(mContext.getResources().getColor(R.color.colorBackgroud));
        paintBlack.setStyle(Paint.Style.FILL);
        paintBlack.setAntiAlias(true);
        mCanvas.drawCircle(basex, basey, raduisMax, paintBlack);
        mCanvas.drawCircle(basex, basey, raduisMax, mPaintRaMax);
        mCanvas.drawCircle(basex, basey, raduisMax+16, mPaintRaMax);
        mCanvas.drawCircle(basex, basey, raduisMin, mPaintRaMin);
    }

    /**
     * 绘制速度文字和速度单位
     */
    private void drawTextView() {
        mPaintText.setTextSize(60 * mDensityDpi);
        float tw = mPaintText.measureText(String.valueOf(speed));//文字的宽度
        mCanvas.drawText(speed + "", basex - tw / 2, (basey + Math.abs(mPaintText.descent() + mPaintText.ascent()) / 4), mPaintText);
        mPaintText.setTextSize(30 * mDensityDpi);
        float tw1 = mPaintText.measureText("km/h");//文字的宽度
        mCanvas.drawText("km/h", basex - tw1 / 2, basey + 60 * mDensityDpi, mPaintText);
    }

    /**
     * 绘制圆盘上的刻度和文字
     */
    private void drawCircleLineView() {
        mPaintRaMax.setStrokeWidth(4);
        for (int i = 0; i < 60; i++) {
            float y = basey + raduisMax;
            if (i % 6 == 0) {
                mCanvas.drawLine(basex, y-2, basex, y - 50 * mDensityDpi, mPaintRaMax);
                mPaintText.setTextSize(40 * mDensityDpi);
                int text = 30 * (i / 6);
                float tw = mPaintText.measureText(String.valueOf(text));
                mCanvas.drawText(text + "", basex - tw / 2, y - 60 * mDensityDpi, mPaintText);
            } else {
                mCanvas.drawLine(basex, y-2, basex, y - 30 * mDensityDpi, mPaintRaMax);
            }
            mCanvas.rotate(6, basex, basey);
        }
    }

    /**
     * 绘制速度变化
     */
    private void drawArcView() {
        RectF rect = new RectF(basex - raduisMax, basey - raduisMax, basex + raduisMax, basey + raduisMax);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        paint.setColor(0x7E3F51B5);
        mCanvas.drawArc(rect, 90, speed*36/30, true, paint);
        //将中心的圆进行覆盖
        Paint paintBlack = new Paint();
        paintBlack.setColor(mContext.getResources().getColor(R.color.colorBackgroud));
        paintBlack.setStyle(Paint.Style.FILL);
        paintBlack.setAntiAlias(true);
        mCanvas.drawCircle(basex, basey, raduisMin, paintBlack);
    }

    public void setSpeedStart(final int newSpeed) {
        if (isRunnaleCk) {
            return;
        }
        isRunnaleCk = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < newSpeed+1; i++) {
                        Thread.sleep(10);
                        speed = i;
                        setSpeed(speed);
                    }
                    isRunnaleCk = false;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void setSpeedEnd() {
        if (isRunnaleCk) {
            return;
        }
        isRunnaleCk = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = speed; i > -1; i--) {
                        Thread.sleep(10);
                        speed = i;
                        setSpeed(speed);
                    }
                    isRunnaleCk = false;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    // 设置速度 并重绘视图
    public void setSpeed(int speed) {
        if (speed > 359 || speed < 0) {
            return;
        }
        this.speed = speed;
        postInvalidate();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        //如果布局里面设置的是固定值,这里取布局里面的固定值;如果设置的是match_parent,则取父布局的大小
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            //如果布局里面没有设置固定值,这里取布局的宽度的1/2
            width = widthSize * 1 / 2;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            //如果布局里面没有设置固定值,这里取布局的高度的3/4
            height = heightSize * 1 / 2;
        }
        if (width > height) {
            height = width;
        } else {
            width = height;
        }
        mWidth = width - (getPaddingBottom() + getPaddingTop());
        mHeight = height - (getPaddingRight() + getPaddingLeft());
        basex = width / 2;
        basey = height / 2;
        raduisMax = mWidth / 2;
        raduisMin = raduisMax / 2;

        setMeasuredDimension(width, height);
    }


}
